package com.quectel.taskserver.job.scheduling;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.func.LambdaUtil;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.module.scheduling.dto.AttendanceDailyGenReq;
import com.quectel.core.module.scheduling.dto.SchedulingAttendanceDailyDto;
import com.quectel.core.module.scheduling.dto.SchedulingAttendanceGroupDto;
import com.quectel.core.module.scheduling.dto.SchedulingAttendanceGroupPersonBindingDto;
import com.quectel.core.module.scheduling.service.SchedulingAttendanceDailyService;
import com.quectel.core.module.scheduling.service.SchedulingAttendanceGroupPersonBindingService;
import com.quectel.core.module.scheduling.service.SchedulingAttendanceGroupService;
import com.quectel.util.common.DateUtils;
import com.quectel.util.kit.PageFetchKit;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shardingsphere.elasticjob.api.ShardingContext;
import org.apache.shardingsphere.elasticjob.simple.job.SimpleJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author rananxin
 * @date 2021/11/18 7:24 下午
 */
@Component
public class SchedulingAttendanceDailyGenTask implements SimpleJob {

    private static final Logger LOGGER = LoggerFactory.getLogger(SchedulingAttendanceDailyGenTask.class);

    @DubboReference
    private SchedulingAttendanceGroupService schedulingAttendanceGroupService;
    @DubboReference
    private SchedulingAttendanceGroupPersonBindingService schedulingAttendanceGroupPersonBindingService;
    @DubboReference
    private SchedulingAttendanceDailyService schedulingAttendanceDailyService;

    @Override
    public void execute(ShardingContext shardingContext) {
        gen();
    }

    /**
     * 每日考勤生成
     * 提前生成下一天的，原因是当天可能提前打卡明天的
     */
    private void gen() {
        LOGGER.debug("每日考勤开始生成");

        final Date currentDate = new Date();
        String format = DateUtils.format(DateUtil.offsetDay(currentDate, 1), DateUtils.FormatType.SIMPLE);

        AttendanceDailyGenReq attendanceDailyGenReq = new AttendanceDailyGenReq();
        attendanceDailyGenReq.setDataStr(format);

        Map<String, Object> params = new HashMap<>();
        params.put(LambdaUtil.getFieldName(SchedulingAttendanceGroupDto::getStatus), SystemConstants.ENABLE.toString());
        List<SchedulingAttendanceGroupDto> allGroup = PageFetchKit.fetchAll(req -> schedulingAttendanceGroupService.queryList(req), params, 500);

        for (SchedulingAttendanceGroupDto schedulingAttendanceGroupDto : allGroup) {
            try {
                if (SystemConstants.DISABLE.equals(schedulingAttendanceGroupDto.getStatus())) {
                    // 考勤组禁用，直接跳过
                    continue;
                }
                List<SchedulingAttendanceGroupPersonBindingDto> schedulingAttendanceGroupPersonBindingDtos = schedulingAttendanceGroupPersonBindingService
                        .selectListByGroupId(schedulingAttendanceGroupDto.getId());
                for (SchedulingAttendanceGroupPersonBindingDto schedulingAttendanceGroupPersonBindingDto : schedulingAttendanceGroupPersonBindingDtos) {
                    SchedulingAttendanceDailyDto attendanceDailyDto = schedulingAttendanceDailyService.selectOne(schedulingAttendanceGroupPersonBindingDto.getPersonId(), format);
                    if (Objects.isNull(attendanceDailyDto)) {
                        attendanceDailyGenReq.setPersonId(schedulingAttendanceGroupPersonBindingDto.getPersonId());
                        schedulingAttendanceDailyService.gen(attendanceDailyGenReq);
                    }
                }
            } catch (Exception e) {
                LOGGER.error("Scheduling attendance daily gen error", e);
            }
        }
    }

}
